import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pplive/common/utils.dart';
import 'package:pplive/models/hot_model.dart';
import 'package:pplive/views/banner_screen.dart';
import 'package:pplive/views/RoomRecommCell.dart';

import '../../views/room_cell.dart';

extension HotExtensions on HotModel {
  List<Widget> createViews() {
    var space = 10.0;
    return [
      topLiveList.createView(),
      SliverToBoxAdapter(child: Container(height: space)),
      SliverToBoxAdapter(
          child: Padding(
        padding: EdgeInsets.fromLTRB(10, 0, 10, 10),
        child: Container(
            height: 194,
            color: Color(0xE1A6FF),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  height: 40,
                  child: Text(
                    "官方推荐",
                    style: Util.shared().theme?.textTheme.titleMedium,
                  ),
                ),
                Expanded(child: liveRecommendList.horizontalViews())
              ],
            )),
      )),
      SliverToBoxAdapter(child: Container(height: space)),
      bannerList.createView(104),
      SliverToBoxAdapter(child: Container(height: space)),
      liveRecommendList.createView(),
      SliverToBoxAdapter(child: Container(height: space)),
      liveList.createView()
    ];
  }
}

extension ListLiveExtensions on List<LiveModel> {
  Widget createView() {
    if (this.isEmpty) {
      return SliverToBoxAdapter();
    }

    return SliverPadding(
        padding: EdgeInsets.all(10),
        sliver: SliverGrid.builder(
            itemCount: this.length,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                mainAxisSpacing: 5,
                crossAxisSpacing: 5,
                childAspectRatio: 181.0 / 237),
            itemBuilder: (c, index) => this[index].createView(index)));
  }

  Widget horizontalViews() {
    if (this.isEmpty) {
      return Container();
    }

    return GridView.builder(
        scrollDirection: Axis.horizontal,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 1,
            crossAxisSpacing: 4,
            mainAxisSpacing: 10,
            childAspectRatio: 144.0 / 106.0),
        itemCount: this.length,
        itemBuilder: (c, index) =>
            this[index].createView(index, isRecomm: true));
  }
}

extension LiveModelExtensions on LiveModel {
  Widget createView(int index, {bool isRecomm = false}) {
    return isRecomm
        ? RoomRecommCell(index: index, live: this)
        : RoomCell(index: index, live: this);
  }
}

extension BannerModelExtensions on List<BannerModel> {
  Widget createView(double height) {
    return SliverToBoxAdapter(
      child: Container(
        height: height,
        decoration:
            BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(12))),
        child: BannerScreen(banners: this),
      ),
    );
  }
}
